package hn.sigit.model.hnd.ladmshadow;

import java.io.Serializable;
import javax.persistence.*;

import hn.sigit.model.commons.IParty;
import hn.sigit.model.ladm.external.ExtParty;
import hn.sigit.model.ladm.party.LA_PartyRoleType;
import hn.sigit.model.ladm.party.LA_PartyType;

@Entity
@org.hibernate.annotations.Proxy(lazy=false)
@Table(name="Party", schema="ladmshadow")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorValue("Party")
public class Party extends AssociationInfo implements IParty, Serializable {
	private static final long serialVersionUID = 1L;
	
	private long pID;
	private String name;
	public ExtParty extParty;
	private LA_PartyRoleType role;
	private LA_PartyType type;
	private java.util.Set<SpatialSource> spatialSources = new java.util.HashSet<SpatialSource>();
	private java.util.Set<PartyMember> partymembers = new java.util.HashSet<PartyMember>();
	private java.util.Set<RRR> rrr = new java.util.HashSet<RRR>();
	private java.util.Set<Source> sources = new java.util.HashSet<Source>();
	private BAUnit baUnit;
	private java.util.Set<Mortgage> mortgages = new java.util.HashSet<Mortgage>();
	private java.util.Set<AdministrativeSource> adminSources = new java.util.HashSet<AdministrativeSource>();
	
	public Party() {
	}
	
	@Id
	@Column(name="ID", nullable=false)
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="shw_party_id_gen")
	@SequenceGenerator(name="shw_party_id_gen", sequenceName="shw_party_id_seq", allocationSize=1)
	public long getpID() {
		return pID;
	}
	protected void setpID(long value) {
		this.pID = value;
	}
	@Transient
	public long getORMID() {
		return getpID();
	}
	
	@OneToOne(targetEntity=ExtParty.class, fetch=FetchType.LAZY)
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})
	@JoinColumns({ @JoinColumn(name="ExtPartyID") })
	@org.hibernate.annotations.LazyToOne(value=org.hibernate.annotations.LazyToOneOption.NO_PROXY)
	public ExtParty getExtParty() {
		return extParty;
	}
	public void setExtParty(ExtParty extParty) {
		this.extParty = extParty;
	}

	@Column(name="Name", nullable=true, length=255)
	public String getName() {
		return name;
	}
	public void setName(String value) {
		this.name = value;
	}
	
	@Column(name="Role", nullable=true)
	@Enumerated(EnumType.STRING)
	public LA_PartyRoleType getRole() {
		return role;
	}
	public void setRole(LA_PartyRoleType value) {
		this.role = value;
	}
	
	@Column(name="Type", nullable=true)
	@Enumerated(EnumType.STRING)
	public LA_PartyType getType() {
		return type;
	}
	public void setType(LA_PartyType value) {
		this.type = value;
	}
	
	@ManyToMany(targetEntity=SpatialSource.class)	
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})	
	@JoinTable(name="SpatialSource_Party", schema="ladmshadow", joinColumns={ @JoinColumn(name="PartyID") }, inverseJoinColumns={ @JoinColumn(name="SpatialSourceSourceID") })	
	@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)	
	public java.util.Set<SpatialSource> getSpatialSources() {
		return spatialSources;
	}
	public void setSpatialSources(java.util.Set<SpatialSource> value) {
		this.spatialSources = value;
	}
	
	@Transient
	public GroupParty[] getgrouppartys() {
		java.util.ArrayList lValues = new java.util.ArrayList(5);
		for(java.util.Iterator lIter = partymembers.iterator();lIter.hasNext();) {
			lValues.add(((PartyMember)lIter.next()).getgroupparty());
		}
		return (GroupParty[])lValues.toArray(new GroupParty[lValues.size()]);
	}
	
	public void removegroupparty(GroupParty agroupparty) {
		PartyMember[] lpartymembers = (PartyMember[])partymembers.toArray(new PartyMember[partymembers.size()]);
		for(int i = 0; i < lpartymembers.length; i++) {
			if(lpartymembers[i].getgroupparty().equals(agroupparty)) {
				partymembers.remove(lpartymembers[i]);
			}
		}
	}
	
	public void addgroupparty(PartyMember aPartyMember, GroupParty agroupparty) {
		aPartyMember.setgroupparty(agroupparty);
		partymembers.add(aPartyMember);
	}
	
	public PartyMember getpartymemberBygroupparty(GroupParty agroupparty) {
		PartyMember[] lpartymembers = (PartyMember[])partymembers.toArray(new PartyMember[partymembers.size()]);
		for(int i = 0; i < lpartymembers.length; i++) {
			if(lpartymembers[i].getgroupparty().equals(agroupparty)) {
				return lpartymembers[i];
			}
		}
		return null;
	}
	
	@OneToMany(mappedBy="party", targetEntity=PartyMember.class)
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})
	@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
	public java.util.Set<PartyMember> getpartymembers() {
		return partymembers;
	}
	public void setpartymembers(java.util.Set<PartyMember> value) {
		this.partymembers = value;
	}
	
	@OneToMany(mappedBy="party", targetEntity=RRR.class)
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})
	@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
	public java.util.Set<RRR> getRrr() {
		return rrr;
	}
	public void setRrr(java.util.Set<RRR> value) {
		this.rrr = value;
	}
	
	@OneToMany(mappedBy="party", targetEntity=Source.class)	
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})	
	@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)	
	public java.util.Set<Source> getSources() {
		return sources;
	}
	public void setSources(java.util.Set<Source> value) {
		this.sources = value;
	}
	
	@OneToOne(mappedBy="baParty", targetEntity=BAUnit.class, fetch=FetchType.LAZY)	
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})	
	@org.hibernate.annotations.LazyToOne(value=org.hibernate.annotations.LazyToOneOption.NO_PROXY)	
	public BAUnit getBaUnit() {
		return baUnit;
	}
	public void setBaUnit(BAUnit value) {
		this.baUnit = value;
	}
	
	@ManyToMany(mappedBy="moneyProvider", targetEntity=Mortgage.class)
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})
	@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
	public java.util.Set<Mortgage> getMortgages() {
		return mortgages;
	}
	public void setMortgages(java.util.Set<Mortgage> value) {
		this.mortgages = value;
	}
	
	@ManyToMany(mappedBy="conveyor", targetEntity=AdministrativeSource.class)
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})
	@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
	public java.util.Set<AdministrativeSource> getAdminSources() {
		return adminSources;
	}
	public void setAdminSources(java.util.Set<AdministrativeSource> value) {
		this.adminSources = value;
	}
	
	public String toString() {
		return super.toString();
	}
	
}
